home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / hl / hl-1.002 / hl-1 / Include / Terminal.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-14  |  6.4 KB  |  206 lines

  1. /* -*- Mode: C -*- */
  2. /* Terminal.h - Video terminal interface
  3.  *        Inspired by the Terminal class described in "C++,
  4.  *        a guide for C programmers", by Sharam Hekmatpour,
  5.  *        (c) 1990 by Prentice Hall of  Australia Pty Ltd.
  6.  *        This version uses termcap and is not hardwired
  7.  *        for a particular terminal type.  Also, I only
  8.  *        implemented a bare terminal class and did not
  9.  *        include the higher level classes (windows,
  10.  *        menus, and forms).
  11.  * Created by Robert Heller on Fri Dec  6 21:10:04 1991
  12.  *
  13.  * ------------------------------------------------------------------
  14.  * Home Libarian by Deepwoods Software
  15.  * Common Header Files
  16.  * ------------------------------------------------------------------
  17.  * Modification History:
  18.  * ------------------------------------------------------------------
  19.  * Contents:
  20.  * ------------------------------------------------------------------
  21.  * 
  22.  * 
  23.  * Copyright (c) 1991,1992 by Robert heller (D/B/A Deepwoods Software)
  24.  *        All Rights Reserved
  25.  * 
  26.  */
  27. #ifndef _TERMINAL_
  28. #define _TERMINAL_
  29. #include <stream.h>
  30. #include <common.h>
  31. #include <ctype.h>
  32. #ifdef MESSYDOS
  33. #include <gppconio.h>
  34. #include <keybd.h>
  35. #else
  36. #include <termcap.h>
  37. #endif
  38.  
  39. #ifndef MESSYDOS
  40. #define PosCode(buf,r,c) CopyCode(buf,tgoto(CM,c,r))
  41. #endif
  42. class CommandScreen;
  43. class EditForm;
  44. class ScrollPrompt;
  45. class LLScrollPrompt;
  46. // Terminal class.  Based on termcap.
  47. class Terminal    {
  48. // Terminal mode magic
  49. #ifdef OSK
  50.     static sgbuf ttym;            // Vanila terminal modes (OSK)
  51.     static sgbuf xttym;            // Operating terminal modes (OSK)
  52. #endif
  53. #ifdef unix
  54.     static struct termios ttym;        // Vanila terminal modes (unix)
  55.     static struct termios xttym;        // Operating terminal modes (unix)
  56. #endif
  57.     static ErrFun errFun;            // user error function
  58.     const  bufSize = 512;            // buffer size
  59.     static char termBuf[bufSize];        // scratch buffer
  60. #ifndef MESSYDOS
  61.     const TCapsLen = 1024;            // Termcap buffer size
  62.     static char TermType[48];        // terminal type name
  63.     static char TCapBuf[TCapsLen];        // termcap buffer
  64.     static char tcapbuf[TCapsLen];        // copy
  65.     const char* const BELL = "\007";    // bell character
  66. #endif
  67.     static Terminal* term;            // self reference
  68. #ifndef MESSYDOS
  69.     // termcap capabilities
  70.     static char* BC;            // backspace character
  71.     static char* UP;            // up line sequence
  72.     static char* CL;            // clear screen
  73.     static char* CM;            // cursor movement
  74.     static char* CE;            // clear to eol
  75.     static char* CD;            // clear to eos
  76.     static char* SO;            // standout start
  77.     static char* SE;            // standout end
  78.     static char* HO;            // home cursor
  79.     static char* KD;            // down arrow key
  80.     static char* KU;            // up arrow key
  81.     static char* KR;            // right arrow key
  82.     static char* KL;            // left arrow key
  83.     static char* KH;            // home key
  84.     static char* KB;            // backspace key
  85.     static char* TI;            // terminal init
  86.     static char* TE;            // terminal de-init
  87.     static short ospeed;            // baud rate (used for padding)
  88.     static char PC;                // pad character
  89.     // copy code to a buffer
  90.     char*  CopyCode    (char* buf,const char* code)
  91.             {strcpy(buf,code); return buf + strlen(code);}
  92.     // write control codes
  93.     void WriteCode(const char*);
  94.     void WriteCodeLines(const char*,int);
  95. #endif
  96.     static short cline;            // current line
  97.     static short ccol;            // current column
  98.     static short lines;            // screen lines
  99.     static short colms;            // screen columns
  100. public:
  101.     // do various things to the terminal
  102. #ifdef MESSYDOS
  103.     inline void RevsPen    ()    {textbackground(LIGHTGRAY);
  104.                      textcolor(BLACK);}
  105.     inline void PlainPen    ()    {textbackground(BLACK);
  106.                      textcolor(WHITE);}
  107.     inline void InitChars    ()     {};
  108.     inline void ExitChars    ()    {};
  109.     inline void Home    ()    {PenPos(0,0);}
  110.     inline void Clear    ()    {clrscr();gotoxy(ccol+1,cline+1);}
  111.     inline void ClearEOL    ()    {clreol();gotoxy(ccol+1,cline+1);}
  112.     inline void ClearEOS    ()    {clreol();
  113.                      for (int r=cline+1;r<lines;r++)
  114.                      {
  115.                          gotoxy(1,r+1);
  116.                          clreol();
  117.                      }
  118.                      gotoxy(ccol+1,cline+1);}
  119.     void Bell    ();
  120.  
  121.     Terminal &put(unsigned char ch);
  122.     inline Terminal &put(unsigned char *s) {
  123.         while (*s) {put(*s);s++;}
  124.         return(*this);
  125.     }
  126.     inline Terminal &put(unsigned char *s,int n)
  127.             {for (int i=0;i<n;i++) put(*s++);return(*this);}
  128. #else
  129.     void RevsPen    ()    {WriteCode(SO);}
  130.     void PlainPen    ()    {WriteCode(SE);}
  131.     void InitChars    ()    {WriteCode(TI);}
  132.     void ExitChars    ()    {WriteCode(TE);}
  133.     void Home    ()    {WriteCode(HO); cline = 0; ccol = 0;}
  134.     void Clear    ()    {if (CL == 0) {
  135.                     WriteCode(HO);WriteCodeLines(CD,lines);
  136.                  } else WriteCodeLines(CL,lines);
  137.                  char buf[50];
  138.                  PosCode(buf,cline,ccol);
  139.                  WriteCode(buf);}
  140.     void ClearEOL    ()    {WriteCode(CE);}
  141.     void CLearEOS    ()    {WriteCodeLines(CD,lines-cline);}
  142.     void Bell    ()    {WriteCode(BELL);}
  143. #endif
  144.     // error handler reference
  145.     inline ErrFun& ErrorFun ()    {return errFun;}
  146.     // Constructor and descructor
  147.            Terminal ();
  148.           ~Terminal ();
  149.     // set cursor position
  150. #ifdef MESSYDOS
  151.     inline void PenPos     (int row,int col)
  152.         {gotoxy(col+1,row+1);cline = row;ccol = col;}
  153. #else
  154.     void PenPos    (int row,int col);
  155. #endif
  156.     // get a key from the keyboard (no echo, no wait for eol, no editing)
  157.     int  GetKey    ();
  158.     // key available??
  159.     Boolean KeyAvailable ();
  160.     // error handler
  161.     void Error    (ErrKind err,const char* msg);
  162.     // display a message
  163.     void Message    (const char* msg);
  164.     // get a text string from the keyboard.  Allows editing
  165.     int  GetLine    (char* buffer,int bufsize,
  166.              const char* terminators = "\n\r");
  167.     // Put a character on the screen someplace
  168.     void PutCharAt    (int row,int col,int ch);
  169.     // Put a string someplace on the screen
  170.     void PutStrAt    (int row,int col,const char* str);
  171.     void PutStrAt    (int row,int col,const short* str);
  172.     char* PutStrInBox (int row,int col,int width,int height,
  173.                const char* str);
  174.     // get a text string from the keyboard.  Allows editing
  175.     inline int  PromptLine    (int row,int col,const char* prompt,
  176.              char* buffer,int bufsize,
  177.              const char* terminators = "\n\r")
  178.         {PutStrAt(row,col,prompt);return(GetLine(buffer,bufsize,
  179.                              terminators));}
  180.     inline Boolean YorNp(int row,int col,const char* prompt)
  181.         {int key;
  182.          PutStrAt(row,col,prompt);
  183.          while (true) {
  184.              key = GetKey();
  185.              if (key == 'y' || key == 'Y') {
  186.                  PutStrAt(cline,ccol,"Yes");
  187.                  return(true);
  188.              } else if (key == 'n' || key == 'N') {
  189.                  PutStrAt(cline,ccol,"No");
  190.                  return(false);
  191.              } else Bell();
  192.          }
  193.         }
  194.     // fork a sub-process (this is here to handle the terminal modes
  195.     int  forkprog    (const char** argv);
  196.     // handle an interrupt
  197.     friend int Interrupt();
  198.     friend class CommandScreen;
  199.     friend class EditForm;
  200.     friend class ScrollPrompt;
  201.     friend class LLScrollPrompt;
  202. };
  203.  
  204. #endif  _TERMINAL_
  205.  
  206.